package com.dong.message.service.impl;

import com.dong.message.bean.Message;
import com.dong.message.provider.MessageProvider;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import com.dong.message.service.MessageSend;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class MessageSendImpl implements MessageSend, ApplicationContextAware {
    private Map<String,MessageProvider> messageProviderMap = new ConcurrentHashMap<>();

    public void send(Message message){
        MessageProvider messageProvider =  messageProviderMap.get(message.getProvider());
        messageProvider.send(message);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        messageProviderMap = applicationContext.getBeansOfType(MessageProvider.class);
    }
}
